﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using FangChanCrm.Services.Interface;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FangChanCrm.Controllers
{
    public class LoginController : Controller
    {
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;
        private readonly It_XiaoXiTiXingBiaoService t_XiaoXiTiXingBiaoService;

        public LoginController(It_RenYuanBiaoService renYuanBiaoService, It_XiaoXiTiXingBiaoService xiaoXiTiXingBiaoService)
        {
            t_RenYuanBiaoService = renYuanBiaoService;
            t_XiaoXiTiXingBiaoService = xiaoXiTiXingBiaoService;
        }

        public IActionResult Login()
        {
            return View();
        }
        public IActionResult LoginChongXin()
        {
            return View();
        }

        //添加Cookies
        protected void SetCookies(string key, string value, int minutes = 4320)
        {
            HttpContext.Response.Cookies.Append(key, value, new CookieOptions
            {
                Expires = DateTime.Now.AddMinutes(minutes)
            });
        }
        //删除Cookies
        protected string DeleteCookies()
        {
            try
            {
                HttpContext.Response.Cookies.Delete("账户");
                HttpContext.Response.Cookies.Delete("密码");
            }
            catch (Exception)
            {
            }
            return "1";
        }
        //查找Cookies
        protected string GetCookies(string key)
        {
            HttpContext.Request.Cookies.TryGetValue(key, out string value);
            if (string.IsNullOrEmpty(value))
                value = string.Empty;
            return value;
        }

        //登录 查询  
        public string DengLuCX()
        {
            string zhuangtai = Request.Form["zhuangtai"];
            t_RenYuanBiao t_HuiYuanBiao = new t_RenYuanBiao();
            List<t_RenYuanBiao> list = t_RenYuanBiaoService.GetList().Where(c => c.ZhangHao == Request.Form["name"] && c.MiMa == Request.Form["pwd"]).ToList();//账号密码查找
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].t_GangWeiBiao = null;
                    list[i].t_ZhanDuiBiao = null;
                }
            }
            if (list.Count != 0)
            {
                if (zhuangtai == "未选中")
                {
                    DeleteCookies();
                }
                else
                {
                    SetCookies("账户", list[0].ZhangHao);
                    SetCookies("密码", list[0].MiMa);
                }
                HttpContext.Session.SetString("loginid", "" + list[0].Id + "");
                HttpContext.Session.SetString("账户", "" + list[0].ZhangHao + "");
                HttpContext.Session.SetString("密码", "" + list[0].MiMa + "");
                //HttpContext.Session.SetString("jihe", "" + list + "");
                return "成功";
            }
            else
            {
                List<t_RenYuanBiao> list2 = t_RenYuanBiaoService.GetList().Where(c => c.ZhangHao == Request.Form["name"]).ToList();//账号查找
                if (list2.Count != 0)
                {
                    return "密码错误";
                }
                else
                {
                    return "账号错误";
                }
            }
        }
        //登录 绑定  调用查找Cookies
        public List<string> bangding()
        {
            List<string> list = new List<string>();
            list.Add(GetCookies("账户"));
            list.Add(GetCookies("密码"));
            return list;
        }
        //登录 退出  删除Cookies
        public void panduan()
        {
            DeleteCookies();
        }
    }
}
